package com.itheima.mall.dubbo.api.ums;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mall.dubbo.mapper.ums.ResourceCategoryMapper;
import com.itheima.mall.dubbo.mapper.ums.ResourceMapper;
import com.itheima.mall.entity.domain.ums.Resource;
import com.itheima.mall.entity.domain.ums.ResourceCategory;
import com.itheima.mall.entity.domain.ums.RoleResourceRelation;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 资源分类表 服务实现类
 * </p>
 *
 * @author qinglang
 * @since 2022-07-05
 */
@DubboService
public class ResourceCategoryImplService extends ServiceImpl<ResourceCategoryMapper, ResourceCategory> implements ResourceCategoryService {

    @Autowired
    private ResourceService resourceService;

    @Autowired
    private ResourceMapper resourceMapper;
    @Autowired
    private RoleResourceRelationService roleResourceRelationService;
    @Override
    public void creat(ResourceCategory resourceCategory) {
        this.save(resourceCategory);
    }

    @Override
    @Transactional
    public void delete(Integer id) {
        this.removeById(id);
        List<Integer> resourceIds = resourceMapper.selectObjs(new LambdaQueryWrapper<Resource>().select(Resource::getId).eq(Objects.nonNull(id), Resource::getCategoryId, id)).stream().map(item -> Integer.valueOf(item.toString())).collect(Collectors.toList());
        resourceService.remove(new LambdaQueryWrapper<Resource>().eq(Objects.nonNull(id),Resource::getCategoryId,id));
//        roleResourceRelationService.remove(new LambdaQueryWrapper<RoleResourceRelation>().in(Objects.nonNull(resourceIds),RoleResourceRelation::getResourceId,resourceIds));
    }

    @Override
    public List<ResourceCategory> listAll() {
        List<ResourceCategory> list = this.list();
        return list;
    }

    @Override
    public void updateRC(ResourceCategory resourceCategory) {
        this.updateById(resourceCategory);
    }
}
